//! \file XmlManager.c
//! \brief TODO: Document
// -----------------------------------------------------------------------------
//! This program is free software; you can redistribute it and/or
//! modify it under the terms of the GNU General Public License
//! as published by the Free Software Foundation; either version 2
//! of the License, or (at your option) any later version.
//!
//! This program is distributed in the hope that it will be useful,
//! but WITHOUT ANY WARRANTY; without even the implied warranty of
//! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//! GNU General Public License for more details.
//!
//! You should have received a copy of the GNU General Public License
//! along with this program; if not, write to the Free Software Foundation,
//! Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// -----------------------------------------------------------------------------

#include "XmlManager.h"
#include "expat.h"
#include <stdio.h> // TODO: temporary for testing, remove once complete

// =====[ Local Definitions ]===================================================
#define dXML_BUFFER_SIZE 8192 //!< TODO: document




// =====[ Local DataTypes ]=====================================================

// =====[ Local Variables ]=====================================================
static XML_Parser Parser = NULL;
static Depth = 0;
static char Buffer[dXML_BUFFER_SIZE];

// =====[ Local Functions ]=====================================================
static void _XmlManager_Start(void *data, const char *el, const char **attr);
static void _XmlManager_End(void *data, const char *el);

// =====[ Local Implementation ]================================================


// =============================================================================
// Implement: XmlManager_Initialize
// -----------------------------------------------------------------------------
void XmlManager_Initialize(void)
{
  // TODO: temporary for compile testing.

  // Create the XML parser.
  Parser = XML_ParserCreate(NULL);
  if (Parser == NULL)
  {
    // TODO: something.
  }

  // TODO: comment
  XML_SetElementHandler(Parser, _XmlManager_Start, _XmlManager_End);
}


// =============================================================================
// _XmlManager_Start
// -----------------------------------------------------------------------------
//! TODO: document
//!
//! \param [in] Arg
//!   TODO: document
//!
//! \retval Exception
//!   The success status of the requested operation.
//!   - eOKAY: Execution occurred as requested.
//!   .
// -----------------------------------------------------------------------------
static void _XmlManager_Start(void *data, const char *el, const char **attr)
{
  int i;

  for (i = 0; i < Depth; i++)
    printf("  ");

  printf("%s", el);

  for (i = 0; attr[i]; i += 2) {
    printf(" %s='%s'", attr[i], attr[i + 1]);
  }

  printf("\n");
  Depth++;
}  /* End of start handler */


// =============================================================================
// FxName
// -----------------------------------------------------------------------------
//! TODO: document
//!
//! \param [in] Arg
//!   TODO: document
//!
//! \retval Exception
//!   The success status of the requested operation.
//!   - eOKAY: Execution occurred as requested.
//!   .
// -----------------------------------------------------------------------------
static void _XmlManager_End(void *data, const char *el)
{
  Depth--;
}  /* End of end handler */


// =============================================================================
// Implement: XmlManager_ParseFile
// -----------------------------------------------------------------------------
int XmlManager_ParseFile(char* pFileName)
{
  FILE* pFile = NULL;
  int Length;
  int Done;

  // Validate our arguments.
  if (pFileName == NULL) return (-1); // A file name was not provided.

  // Attempt to open the requested file.
  fopen_s(&pFile, pFileName, "r");
  if (pFile == NULL) return (-1); // Error opening the file.

  // Iterate through the entire contents of the file.
  Done = 0; // Reset our completion marker.
  while (Done == 0)
  {
    // Open the file stream and determine the length of the file.
    Length = fread(&(Buffer[0]), 1, dXML_BUFFER_SIZE, pFile);

    // If there was an error, return.
    if (ferror(pFile))
    {
      fprintf(stderr, "Read error\n");
      fclose(pFile); // Close the file.
      return (-1);
    }

    // Update our completion marker.
    Done = feof(pFile);

    // XML parse the data buffer that was read in.
    if (!XML_Parse(Parser, Buffer, Length, feof(pFile)))
    {
      fprintf(stderr, "Parse error at line %d:\n%s\n",
              XML_GetCurrentLineNumber(Parser),
              XML_ErrorString(XML_GetErrorCode(Parser)));
      fclose(pFile); // Close the file.
      return (-1);
    }
  }

  // Close the file.
  fclose(pFile);

  return (0);

}
